package com.lcj.dr.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lcj.dr.controller.form.DriverFineForm;
import com.lcj.dr.controller.form.SearchFineForm;
import com.lcj.dr.db.dao.DriverDao;
import com.lcj.dr.db.dao.DriverFineDao;
import com.lcj.dr.db.pojo.DriverFineEntity;
import com.lcj.dr.service.DriverFineService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DriverFineServiceImpl implements DriverFineService {

    @Resource
    private DriverFineDao driverFineDao;

    @Resource
    private DriverDao driverDao;

    @Override
    public Integer addOrUpdate(DriverFineForm form) {
        int rows = 0;
        if(form.getId()!=null) {
            Map<String, Object> param = BeanUtil.beanToMap(form);
            rows = driverFineDao.updateFine(param);
        } else {
            Map param = new HashMap();
            param.put("driverId", form.getDriverId());
            long driverCount = driverDao.hasDriver(param);
            if(driverCount == 0) {
                return 0;
            }
            rows = driverFineDao.insertFine(form);
        }
        return rows;
    }

    @Override
    public Integer deleteFine(DriverFineForm form) {
        Integer rows = driverFineDao.deleteFine(form);
        return rows;
    }

    @Override
    public PageInfo<DriverFineEntity> selectPage(SearchFineForm form) {
        PageHelper.startPage(form.getPage(), form.getLength());
        Map<String, Object> param = BeanUtil.beanToMap(form);
        List<DriverFineEntity> list = driverFineDao.selectDriverFine(param);
        PageInfo<DriverFineEntity> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

}
